package tcp

import (
	"alarm_service/common/logger"
	"alarm_service/internal/repo/command"
	"alarm_service/internal/repo/device"
	"errors"
	"fmt"
	"net"
	"strconv"
)

type CallBackCase struct {
	deviceRepo device.Repo
	command.Repo
}

func NewCallBackCase(dr device.Repo, cr command.Repo) *CallBackCase {
	s := &CallBackCase{
		deviceRepo: dr,
		Repo:       cr,
	}
	return s
}

func (s *CallBackCase) SetPhoneCallBack(packet []byte, conn net.Conn) error {
	data := string(packet)
	identifier := data[0:15]
	deviceInfo, err := s.deviceRepo.GetDeviceByIdentifier(fmt.Sprintf("0%s", identifier))
	if err != nil {
		logger.Errorf("[SetPhoneCallBack] unkonw device: %v", err)
		return errors.New("[SetPhoneCallBack]找不到设备")
	}

	phoneStr := data[15:]
	phones := []string{}
	for i := 0; i < len(phoneStr); {
		d := i + 11
		phones = append(phones, phoneStr[i:d])
		i = d
	}

	err = s.deviceRepo.SetPhones(deviceInfo.ID, phones)
	if err != nil {
		logger.Infof("[SetPhoneCallBack] update %d err %v", deviceInfo.ID, err)
		return errors.New("[SetPhoneCallBack] update err")
	}
	return nil
}

func (s *CallBackCase) SetMessageSwitchCallBack(packet []byte, conn net.Conn) error {
	data := string(packet)
	identifier := data[0:15]
	deviceInfo, err := s.deviceRepo.GetDeviceByIdentifier(fmt.Sprintf("0%s", identifier))
	if err != nil {
		logger.Errorf("[SetMessageSwitchCallBack] unkonw device: %v", err)
		return errors.New("[SetMessageSwitchCallBack]找不到设备")
	}

	onOff, err := strconv.Atoi(string(data[15]))
	if err != nil {
		logger.Errorf("[SetMessageSwitchCallBack] unkonw onOff: %v", string(data[15]))
		return errors.New("[SetMessageSwitchCallBack] unkonw onOff")
	}

	err = s.deviceRepo.SetMessageSwitch(deviceInfo.ID, uint8(onOff))
	if err != nil {
		logger.Infof("[SetMessageSwitchCallBack] update %d err %v", deviceInfo.ID, err)
		return errors.New("[SetMessageSwitchCallBack] update err")
	}
	return nil
}

func (s *CallBackCase) SetRecoverCallBack(packet []byte, conn net.Conn) error {
	data := string(packet)
	identifier := data[0:15]
	deviceInfo, err := s.deviceRepo.GetDeviceByIdentifier(fmt.Sprintf("0%s", identifier))
	if err != nil {
		logger.Errorf("[SetPhoneCallBack] unkonw device: %v", err)
		return errors.New("[SetPhoneCallBack]找不到设备")
	}

	err = s.deviceRepo.SetRecover(deviceInfo.ID)
	if err != nil {
		logger.Infof("[SetRecoverCallBack] update %d err %v", deviceInfo.ID, err)
		return errors.New("[SetRecoverCallBack] update err")
	}
	return nil
}

func (s *CallBackCase) SetPhoneSwitchCallBack(packet []byte, conn net.Conn) error {
	data := string(packet)
	identifier := data[0:15]
	deviceInfo, err := s.deviceRepo.GetDeviceByIdentifier(fmt.Sprintf("0%s", identifier))
	if err != nil {
		logger.Errorf("[SetPhoneCallBack] unkonw device: %v", err)
		return errors.New("[SetPhoneCallBack]找不到设备")
	}
	onOff, err := strconv.Atoi(string(data[15]))
	if err != nil {
		logger.Errorf("[SetMessageSwitchCallBack] unkonw onOff: %v", string(data[15]))
		return errors.New("[SetMessageSwitchCallBack] unkonw onOff")
	}

	err = s.deviceRepo.SetPhoneSwitch(deviceInfo.ID, uint8(onOff))
	if err != nil {
		logger.Infof("[SetPhoneSwitchCallBack] update %d err %v", deviceInfo.ID, err)
		return errors.New("[SetPhoneSwitchCallBack] update err")
	}
	return nil
}
